﻿#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
using Eb.MessageBox;
#endregion

public partial class ucUtilityMove : System.Web.UI.UserControl
{
    UserManager _utilityManager = new UserManager();

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnUpdate.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnUpdate.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        if (!IsPostBack)
        {
            InitializeDDL();
        }
    }

    protected void btnMove1_Click(object sender, EventArgs e)
    {
        if (lstGroup2.SelectedIndex == -1)
        {
            MessageBox.Show("Select Utility");
            //Response.Write("<script>alert('Select Utility')</script>");
            return;
        }
        lstGroup1.Items.Add(new ListItem(lstGroup2.SelectedItem.ToString(), lstGroup2.SelectedValue));
        lstGroup2.Items.RemoveAt(lstGroup2.SelectedIndex);
    }

    protected void btnMove2_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstGroup2.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstGroup1.Items.Count; j++)
                if (lstGroup2.Items[i].Value == lstGroup1.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstGroup1.Items.Add(new ListItem(lstGroup2.Items[i].Text, lstGroup2.Items[i].Value));
            }
        }
        lstGroup2.Items.Clear();
    }
   
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Update();
    }

    protected void ddlUtilityGroup1_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadData("group1");
    }

    protected void ddlUtilityGroup2_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadData("group2");
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < lstGroup2.Items.Count; i++)

            Response.Write("<script>alert('" + lstGroup2.Items[i].Value + "')</script>");
    }

    protected void lnbHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserHome.aspx");
    }

    protected void btnMove4_Click(object sender, EventArgs e)
    {
        if (lstGroup1.SelectedIndex == -1)
        {
            MessageBox.Show("Select utility");
           // Response.Write("<script>alert('Select Utility')</script>");
            return;
        }
        lstGroup2.Items.Add(new ListItem(lstGroup1.SelectedItem.ToString(), lstGroup1.SelectedValue));
        lstGroup1.Items.RemoveAt(lstGroup1.SelectedIndex);
    }

    protected void btnMove3_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstGroup1.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstGroup2.Items.Count; j++)
                if (lstGroup1.Items[i].Value == lstGroup2.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstGroup2.Items.Add(new ListItem(lstGroup1.Items[i].Text, lstGroup1.Items[i].Value));
            }
        }
        lstGroup1.Items.Clear();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucUtilityGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void btnBack_Click1(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucSearchUtilityGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    #endregion

    #region Methods

    void InitializeDDL()
    {
        DataSet dsGroup = _utilityManager.GetData("UtilityGroup", string.Empty);

        ddlUtilityGroup1 .DataSource = dsGroup;
        ddlUtilityGroup1.DataTextField = "UG_GroupName";
        ddlUtilityGroup1.DataValueField = "UG_GroupID";
        ddlUtilityGroup1.DataBind();
        ddlUtilityGroup1.Items.Insert(0, "Select");

        ddlUtilityGroup2.DataSource = dsGroup;
        ddlUtilityGroup2.DataTextField = "UG_GroupName";
        ddlUtilityGroup2.DataValueField = "UG_GroupID";
        ddlUtilityGroup2.DataBind();
        ddlUtilityGroup2.Items.Insert(0, "Select");
    }

    void LoadData(string group)
    {
       
        if (group == "group1")
        {
            lstGroup1.Items.Clear();
            if (ddlUtilityGroup1.SelectedIndex == 0)
        
                return;
          
            string tableName = "UtilityList";
            string condition = "UT_UtilityGroupID = '" + ddlUtilityGroup1.SelectedValue + "'";

            DataSet dsLoad = _utilityManager.GetData(tableName, condition);
            //for (int i = 0; i < dsLoad.Tables[0].Rows.Count; i++)
            //{
            //    lstGroup1.Items.Add(new ListItem(dsLoad.Tables[0].Rows[i][1].ToString(), dsLoad.Tables[0].Rows[i][0].ToString()));
            //}

            lstGroup1.DataSource = dsLoad;
            lstGroup1.DataTextField = "UT_UtilityName";
            lstGroup1.DataValueField = "UT_UtilityID";
            lstGroup1.DataBind();

        }
        else
        {
            lstGroup2.Items.Clear();
            if (ddlUtilityGroup2.SelectedIndex == 0)
           
                return;
                     
            string tableName = "UtilityList";
            string condition = "UT_UtilityGroupID = '" + ddlUtilityGroup2.SelectedValue + "'";

            DataSet dsLoad = _utilityManager.GetData(tableName, condition);
            //for (int i = 0; i < dsLoad.Tables[0].Rows.Count; i++)
            //{
            //    lstGroup2.Items.Add(new ListItem(dsLoad.Tables[0].Rows[i][1].ToString(), dsLoad.Tables[0].Rows[i][0].ToString()));
            //}

            lstGroup2.DataSource = dsLoad;
            lstGroup2.DataTextField = "UT_UtilityName";
            lstGroup2.DataValueField = "UT_UtilityID";
            lstGroup2.DataBind();

        }
       

    }

    /// <summary>
    /// Update the Utility Group
    /// </summary>
    void Update()
    {
        try
        {
            if ((ddlUtilityGroup1.SelectedIndex==0) || (ddlUtilityGroup2.SelectedIndex==0))
            {
                MessageBox.Show("Select Utility Groups");
               // Response.Write("<script>alert('Select Utility Groups')</script>");
                return;
            }
            else if (ddlUtilityGroup1.SelectedValue == ddlUtilityGroup2.SelectedValue)
            {
                MessageBox.Show("Select Different Utility Groups");
                //Response.Write("<script>alert('Select Different Utility Groups')</script>");
                return;
            }

            string groupID, condition;
            string[] utilities;

            if (lstGroup1.Items.Count == 0)
                MessageBox.Show("No Utility in First Group");
            //Response.Write("<script>alert('No Utility in First Group')</script>");
            else
            {
                condition = "UT_UtilityID = " + lstGroup1.Items[0].Value + "";
                for (int i = 1; i < lstGroup1.Items.Count; i++)
                {
                    condition = condition + " or UT_UtilityID = " + lstGroup1.Items[i].Value + "";
                }
                groupID = ddlUtilityGroup1.SelectedValue;
                if (_utilityManager.AddRemoveUtility(condition, groupID))
                    MessageBox.Show("Successfully Added");
                   // Response.Write("<script>alert('Added Successfully!')</script>");
            }

            ////
            if (lstGroup2.Items.Count == 0)
                MessageBox.Show("No Utility in Second Group.");
            // Response.Write("<script>alert('No Utility in Second Group')</script>");
            else
            {
                condition = "UT_UtilityID = " + lstGroup2.Items[0].Value + "";
                for (int i = 1; i < lstGroup2.Items.Count; i++)
                {
                    condition = condition + " or UT_UtilityID = " + lstGroup2.Items[i].Value + "";
                }
                groupID = ddlUtilityGroup2.SelectedValue;
                if (_utilityManager.AddRemoveUtility(condition, groupID))
                    MessageBox.Show("Successfully Added");
                  //  Response.Write("<script>alert('Added Successfully!')</script>");
            }
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Utility Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    #endregion


}
